package com.itheima.service.impl;

import com.itheima.bean.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.io.InputStream;
@Service("userService")
@Transactional
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper mapper;

    /**
     * 定义登录的方法
     * @param user 封装用于登录的用户名和密码
     * @return 返回查到的用户信息
     */
    @Override
    public User login(User user) {
        /*SqlSession sqlSession = null;
        User existUser = null;
        try {
            //1 加载核心配置文件资源
            InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");
            //2 获取SqlSessionFactory对象
            SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
            //3 获取SqlSession对象
            sqlSession = factory.openSession(true);
            //4 获取mapper的代理对象，执行操作
            UserMapper mapper = sqlSession.getMapper(UserMapper.class);
            existUser = mapper.login(user);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //5 释放资源
            if(sqlSession!=null){
                sqlSession.close();
            }
        }
        //6 返回结果*/
        return mapper.login(user);
    }
}
